package com.fs.web.youzan.controller;

import com.alibaba.fastjson.JSON;
import com.fs.web.base.enums.CodeEnum;
import com.fs.web.base.utils.ApplicationUtils;
import com.fs.web.base.vo.DataGrid;
import com.fs.web.base.vo.JsonResponse;
import com.fs.web.base.vo.SysPath;
import com.fs.web.youzan.utils.YouzanUtils;
import com.youzan.cloud.open.sdk.gen.v4_0_0.model.YouzanTradesSoldGetParams;
import com.youzan.cloud.open.sdk.gen.v4_0_0.model.YouzanTradesSoldGetResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 有赞订单管理
 */
@Slf4j
@Controller
@RequestMapping("/console/youzanOrder/rewrite")
public class YouzanOrderController {
    @Autowired
    private YouzanUtils youzanUtils;

    @RequestMapping("/order")
    public String shop(){
        return SysPath.BACK_PATH.concat("/youzan").concat("/order");
    }

    @ResponseBody
    @RequestMapping("/list")
    public JsonResponse list(HttpServletRequest request){
        try {
            Map<String, Object> map = ApplicationUtils.getRequestDatas();
            YouzanTradesSoldGetParams youzanTradesSoldGetParams = JSON.parseObject(JSON.toJSONString(map), YouzanTradesSoldGetParams.class);
            YouzanTradesSoldGetResult order = youzanUtils.getOrder(youzanTradesSoldGetParams);
            if(!order.getSuccess()){
                return JsonResponse.error("网络错误,获取失败");
            }
            DataGrid dataGrid = DataGrid.gridBuilder(order.getData().getFullOrderInfoList());
            return JsonResponse.success(dataGrid);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return JsonResponse.error(CodeEnum.EX999);
        }
    }
}
